<?php

// 调试模式
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "=== Starting Database Connection Test ===\n";

// 1. 检查vendor/autoload.php
$autoloadPath = __DIR__.'/../vendor/autoload.php';
if (!file_exists($autoloadPath)) {
    die("❌ Error: vendor/autoload.php not found at $autoloadPath\n"
       ."Please run 'composer install' first.\n");
}
require_once $autoloadPath;
echo "✓ vendor/autoload.php loaded\n";

// 2. 检查bootstrap/database.php
$dbConfigPath = __DIR__.'/../bootstrap/database.php';
if (!file_exists($dbConfigPath)) {
    die("❌ Error: bootstrap/database.php not found at $dbConfigPath\n"
       ."Please check your database configuration file.\n");
}

try {
    // 3. 初始化数据库连接
    $capsule = require $dbConfigPath;
    echo "✓ Database connection initialized\n";

    // 4. 获取PDO连接
    $pdo = $capsule->getConnection()->getPdo();

    // 5. 测试college表存在
    $stmt = $pdo->query("SELECT COUNT(*) FROM college");
    $count = $stmt->fetchColumn();
    echo "✓ College table exists with {$count} records\n";

    // 6. 测试字段存在
    $stmt = $pdo->query("DESCRIBE college");
    $columns = $stmt->fetchAll(PDO::FETCH_COLUMN);

    $requiredColumns = ['school_id', 'college_id'];
    foreach ($requiredColumns as $column) {
        if (!in_array($column, $columns)) {
            throw new Exception("Missing required column: {$column}");
        }
    }
    echo "✓ All required columns exist\n";

    echo "✅ All tests passed successfully\n";
    exit(0);

} catch (\Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
    exit(1);
}
